<template>
	<view class="spread">
		<view class="head">
			<img class="img" :src="userinfo.headpic"></img>
		</view>
		<view class="title">{{userinfo.nickName}}</view>
		<view class="er">
			<img :src="getimg + userinfo.qrcode"></img>
		</view>
		<view class="btn">
			<view>
				<button class="butt" @click="save" :disabled="is_click">保存二维码</button>
				<button class="butt" style="margin-top: 30upx;" @click="invite_code()">绑定邀请人</button>
			</view>
		</view>
		<view class="gui">
			<view>1.将此页面二维码保存并发送给您的好友</view>
			<view>2.好友识别二维码通过手机号进行注册</view>
			<view>3.好友进行实名认证后返推荐奖励给您账户</view>
		</view>
	</view>
</template>

<script>
	import UserModel from '../../model/user.js'
	export default {
		data() {
			return {
				getimg: this.$get_img,
				code_url: '',
				is_click: false,
				userinfo: {}
			};
		},
		onLoad() {
			this._load()
		},
		methods: {

			_load() {
				UserModel.get_qrcode('app').then(res=>{
					this.userinfo=res.data
				})
			},
			save() {
				const that = this
				// #ifdef H5
				uni.showModal({
					title: '请截图保存',
				})
				// #endif

				
				this.is_click = true
				uni.showLoading({
					title: '加载中'
				});
				uni.downloadFile({
							url: that.getimg + that.code_url, //仅为示例，并非真实的资源
							success: (res) => {
								uni.saveImageToPhotosAlbum({
									filePath: res.tempFilePath,
									success: function(res) {
										console.log('save success', res);
										uni.showToast({
											title: '图片保存成功',
											duration: 2000
										})
										that.is_click = false
										uni.hideLoading();
									}
								});
							}
						});

				

	



			},
			invite_code(){
				uni.scanCode({
				    success: function (res) {
				     //   console.log('条码类型：' + res.scanType);
				       console.log('条码内容：' + res.result);
					   let url=res.result
					   if(url.indexOf('?')!=-1){
					    var str=url.split("?")[1];
					   	console.log(str)
					   	 var strs=str.split("&");
						 var data=new Object();
					   	for(var i=0;i<strs.length;i++){
					   		data[strs[i].split("=")[0]]=strs[i].split("=")[1];
					   	}
						
	
					   }
					   else{
							return;
					   }
					 
						UserModel.bind_user_id(data.value).then(res=>{
							console.log(res)
							if(res.code==200){
								uni.showToast({
									title:'绑定成功'
								})
							}
						})
				    }
				});
				
			}
		},
		onPullDownRefresh() {
			//this._load()
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 2000);
		}
	}
</script>

<style lang="scss">
	.spread {
		.head {
			background-color: #FA436A;
			height: 100px;
			position: relative;

			.img {
				width: 65px;
				height: 65px;
				border-radius: 50px;
				border: 1px solid #C896A6;
				position: absolute;
				left: 50%;
				bottom: -30px;
				margin-left: -32.5px;
			}
		}

		.title {
			text-align: center;
			padding: 40px 0 30px;
			font-size: 18px;
			font-weight: 600;
		}

		.er {
			text-align: center;

			img {
				width: 180px;
				height: 180px;
				border: 2px solid #F6D7B1;
				padding: 2px;
				margin: 0px 0 25px;
			}
		}

		.btn {
			display: flex;
			font-size: 13px;
			justify-content: center;

			.butt {
				background-color: #FB6471;
				height: 25px;
				line-height: 25px;
				border-radius: 20px;
				width: 250rpx;
				text-align: center;
				color: #fff;
			}
		}

		.gui {
			color: #9E9E9E;
			font-size: 12px;
			padding: 40px 0 0 75px;
			line-height: 20px;
		}
	}
</style>
